<template>
  <div>
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"/>
      </el-form-item>
      <el-form-item v-if="!form.id" label="密码" prop="password">
        <el-input v-model="form.password" show-password/>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"/>
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-select v-model="form.roleId" placeholder="请选择">
          <el-option
              v-for="item in formDefault.role"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="头像">
        <el-upload
            action="#"
            :auto-upload="false"
            :limit="1"
            :multiple="false"
            list-type="picture-card"
            accept="image/*"
            :on-change="changeFile"
            :on-remove="removeFile"
            :file-list="fileList"
            :class="{hide: hideUpload}">
          <i class="el-icon-plus"/>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="small" @click="submit()">提交</el-button>
        <el-button type="danger" size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, post, asyncGet } from "@/plugins/axios"
import { Message } from "element-ui"

export default {
  name: "edit",
  props: ['id', 'bankId'],
  created() {
    if (this.id != null) {
      this.getOne()
    }
    this.getRoles()
  },
  data() {
    const module = '/user'
    const check = async (rule, value, callback) => {
      const response = await asyncGet(module + '/check', {id: this.id, value: value})
      if (response.data.data) {
        callback()
      } else {
        callback(new Error('已存在相同数据'))
      }
    }
    return {
      uri: {
        getOne: module + '/selectOne/',
        saveOrUpdate: module + '/saveOrUpdate',
        getRoles: '/role/selectAll'
      },
      form: {
        username: '',
        password: '',
        phone: '',
        email: '',
        roleId: '',
        avatar: ''
      },
      formDefault: {
        role: []
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 30, message: '用户名不能超过30个字符', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的手机号格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        roleId: [
          {required: true, message: '请选择用户角色', trigger: 'blur'}
        ]
      },
      fileList: [],
      hideUpload: false
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.password === null || this.form.password === '' || this.form.password === undefined) {
            delete this.form.password
          }
          post(this.uri.saveOrUpdate, this.form, response => {
            if (response.code === 200) {
              Message.success(response.message)
            }
            this.$emit('update:editDialogShow',false)
            this.$emit('getUserData')
          })
        } else {
          return false
        }
      });
    },
    cancel() {
      this.$emit('update:editDialogShow',false)
    },
    getOne() {
      get(this.uri.getOne + this.id, {}, response => {
        for (const key in this.form) {
          this.form[key] = response.data[key]
        }
        this.form.avatar = response.data.avatarPath
        this.form.id = response.data.id
        this.fileList.push({url: this.imgUrl + this.form.avatar})
        if (this.fileList[0].url) {
          this.hideUpload = true
        } else {
          this.hideUpload = false
        }
      })
    },
    getRoles() {
      get(this.uri.getRoles, {status: 1}, response => {
        const roles = response.data;
        for (const item in roles) {
          this.formDefault.role.push({
            label: roles[item].roleName,
            value: roles[item].id
          })
        }
      })
    },
    changeFile(file) {
      this.form.avatar = file.raw
      this.hideUpload = true
    },
    removeFile() {
      this.form.avatar = null
      this.hideUpload = false
    }
  }
}
</script>

<style scoped lang="less">
.hide {
  /deep/
  .el-upload--picture-card {
    display: none;
  }
}
</style>